package com.algorithm;

/**
 * 买卖股票的最佳时间
 */
public class BestTimeToBuyAndSellStock {

    public static int maxProfit(int[] prices) {
        int lowPrice = 0;
        int totalPrice = 0;
        for (int i = 0; i < prices.length; i++) {
            if (i==0||prices[i]<lowPrice){
                lowPrice=prices[i];
            }
            if (i==0|prices[i]-lowPrice>totalPrice){
                totalPrice=prices[i]-lowPrice;
            }
        }
        return totalPrice;
    }

    public static void main(String[] args) {
        int [] nums=new int[]{7,1,5,3,6,4};
        int result = maxProfit(nums);
        System.out.println(result);
    }
}
